package homework14;

/*
自定义员工类
*/

public class Work1_Staff {
    //1.私有化成员变量
    private String name;
    private int age;

    //3.公有化有参无参构造方法
    public Work1_Staff() {
    }
    public Work1_Staff(String name, int age) throws Work1_AgeException, Work1_NullException {
        setName(name);
        setAge(age);
    }
    public String getName() {
        return name;
    }
    //2.公有化get和set方法
    public void setName(String name) throws Work1_NullException {
        //做成员变量name的合理值判断，不合理则抛出空异常
        if(null == name || "" == name) {
            throw new Work1_NullException("姓名不合理哦！！！");
        } else {
            this.name = name;
        }
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) throws Work1_AgeException {
        //做成员变量age的合理值判断，不合理则抛出年龄异常
        if(age < 18 || age > 150) {
            throw new Work1_AgeException("年龄不合理哦！！！");
        } else {
            this.age = age;
        }
    }

    //4.重写toString方法
    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
